/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servicios.Managers;

import java.util.List;
import modelo.Articulo;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import servicios.DAO.ArticuloDAO;

/**
 *
 * @author epl
 */
public class ArticuloManager {
    public ArticuloDAO dao;
    public Articulo articulo;

    public Articulo getArticulo() {
        return articulo;
    }

    public void setArticulo(Articulo articulo) {
        this.articulo = articulo;
    }
   
    public ArticuloDAO getDao() {
        return dao;
    }

    public void setDao(ArticuloDAO dao) {
        this.dao = dao;
    }

    public Articulo getNewArticulo(){
        Resource r = new ClassPathResource("etc/beans.xml");
        BeanFactory factory = new XmlBeanFactory(r);

        Articulo art = (Articulo) factory.getBean("articulo");
        return art;
    }
    public void save (){
        //TODO argregar validacion
        this.getDao().save(articulo);
    }
    public void validarArticulo(){
        //TODO agregar proceso de validacion posible delegacion
    }
    public Articulo getArticuloById(long id){
        return this.getDao().get(id);
    }

    public List<Articulo> findArticulosByDescripcion (String textoContenido){
        return this.dao.getByDescripcion(textoContenido);
    }

    public Articulo findFristArticuloByDescripcion (String textoContenido){
        List<Articulo> articulos = this.dao.getByDescripcion(textoContenido);
        return articulos.get(0);
    }

    public List<Articulo> findArticulosByPrecio (float precio){
        return this.dao.getByPrecio(precio);
    }

    public Articulo findFristArticuloByPrecio (float precio){
        List<Articulo> articulos = this.dao.getByPrecio(precio);
        return articulos.get(0);
    }
    
}
